
#import "PUReportVC.h"
#import "PUReportCell.h"
#import "UITextView+YLTextView.h"
#import "IQKeyboardManager.h"
@interface PUReportVC ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic,retain) UICollectionView *collectView;
@property (nonatomic,retain) NSMutableArray *reportData;
@property (nonatomic,retain) UIButton *tjBtn;
@property (nonatomic,retain) UITextView *textView;
@property (nonatomic,retain) UILabel *countLab;
@property (nonatomic)NSInteger selectItem;
@property (nonatomic)BOOL isSelect;
@end

@implementation PUReportVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"举报";
    self.view.backgroundColor=[UIColor whiteColor];
    self.isSelect=NO;
    self.reportData=[NSMutableArray arrayWithArray:@[@"涉及政治",@"色情低俗内容",@"未成年",@"危害未成年健康内容",@"广告营销信息",@"不文明语言",@"诈骗恶意诱导",@"其他"]];
    
    UIView *xian1=[[UIView alloc]init];
    xian1.backgroundColor=[UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
    xian1.frame=CGRectMake(0, 0,self.view.bounds.size.width, 1);
    [self.view addSubview:xian1];
    
    UIView *view1=[[UIView alloc]init];
    view1.backgroundColor=[UIColor whiteColor];
    view1.frame=CGRectMake(0, xian1.frame.origin.y+xian1.frame.size.height,self.view.bounds.size.width, 48*KWidthScale);
    view1.userInteractionEnabled=YES;
    [self.view addSubview:view1];
    
    UIView *leftView1=[[UIView alloc]init];
    leftView1.backgroundColor=[UIColor colorWithRed:255/255.0 green:114/255.0 blue:154/255.0 alpha:1];
    leftView1.frame=CGRectMake(12*KWidthScale, 16*KWidthScale,3*KWidthScale, 16*KWidthScale);
    [view1 addSubview:leftView1];
    
    UILabel *lab1 = [[UILabel alloc] init];
    lab1.frame = CGRectMake(23*KWidthScale, 0,150, view1.frame.size.height);
    lab1.text=@"举报类型";
    lab1.font=[UIFont systemFontOfSize:16];
    lab1.textColor=[UIColor blackColor];
    [view1 addSubview:lab1];
    
    UIView *xian3=[[UIView alloc]init];
    xian3.backgroundColor=[UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
    xian3.frame=CGRectMake(0, view1.frame.origin.y+view1.frame.size.height,self.view.bounds.size.width, 1);
    [self.view addSubview:xian3];
    
    //加载collectionView视图
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;//垂直方向
    _collectView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
//    _collectView.showsHorizontalScrollIndicator = NO;
    _collectView.delegate = self;
    _collectView.dataSource = self;
//    _collectView.backgroundColor =[UIColor blueColor];
    _collectView.scrollEnabled = YES;
     //注册cell重用标识
     [self.collectView registerClass:[PUReportCell class] forCellWithReuseIdentifier:@"FLReportCell"];
    [self.view addSubview:self.collectView];
    
    self.collectView.frame=CGRectMake(0,55*KWidthScale, _window_width, 170*KWidthScale);
    
    UIView *xian4=[[UIView alloc]init];
    xian4.backgroundColor=[UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
    xian4.frame=CGRectMake(0, self.collectView.frame.origin.y+self.collectView.frame.size.height+12*KWidthScale,self.view.bounds.size.width, 1);
    [self.view addSubview:xian4];
    
    UIView *view2=[[UIView alloc]init];
    view2.backgroundColor=[UIColor whiteColor];
    view2.frame=CGRectMake(0, xian4.frame.origin.y+xian4.frame.size.height,self.view.bounds.size.width, 48*KWidthScale);
    view2.userInteractionEnabled=YES;
    [self.view addSubview:view2];
    
    UIView *leftView2=[[UIView alloc]init];
    leftView2.backgroundColor=[UIColor colorWithRed:255/255.0 green:114/255.0 blue:154/255.0 alpha:1];
    leftView2.frame=CGRectMake(12*KWidthScale, 16*KWidthScale,3*KWidthScale, 16*KWidthScale);
    [view2 addSubview:leftView2];
    
    UILabel *lab2 = [[UILabel alloc] init];
    lab2.frame = CGRectMake(23*KWidthScale, 0,150, view2.frame.size.height);
    lab2.text=@"举报类型";
    lab2.font=[UIFont systemFontOfSize:16];
    lab2.textColor=[UIColor blackColor];
    [view2 addSubview:lab2];
    
    UIView *xian5=[[UIView alloc]init];
    xian5.backgroundColor=[UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
    xian5.frame=CGRectMake(0, view2.frame.origin.y+view2.frame.size.height,self.view.bounds.size.width, 1);
    [self.view addSubview:xian5];
    
    self.textView=[[UITextView alloc]init];
    self.textView.placeholder = @"请输入不少于2个字的举报说明";//占位符
    self.textView.font = [UIFont systemFontOfSize:14];
    self.textView.backgroundColor=[UIColor colorWithHex:0xF9F9F9];
    self.textView.layer.cornerRadius = 10;
    self.textView.layer.masksToBounds=YES;
    
    self.textView.layer.borderColor = [UIColor colorWithHex:0xE9EBED].CGColor;
    self.textView.layer.borderWidth = 1.0;
   
    [self.view addSubview:self.textView];
    
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(xian5.mas_bottom).offset(12*KWidthScale);
        make.left.equalTo(self.view.mas_left).offset(12*KWidthScale);
        make.right.equalTo(self.view.mas_right).offset(-12*KWidthScale);
        make.width.height.mas_equalTo(200*KWidthScale);
    }];
    
    
    self.countLab=[[UILabel alloc]init];
//    self.countLab.frame=CGRectMake(self.bgView.frame.size.width*0.053, self.bgView.frame.size.height*0.543, self.bgView.frame.size.width/2-self.bgView.frame.size.width*0.053, self.bgView.frame.size.height*0.195);
    self.countLab.font=[UIFont systemFontOfSize:14];
    self.countLab.textAlignment=NSTextAlignmentRight;
    self.countLab.textColor=[UIColor colorWithHex:0x999999];
    self.countLab.userInteractionEnabled=NO;
    [self.view addSubview:self.countLab];
    
    [self.countLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self.textView.mas_bottom).offset(-12*KWidthScale);
//        make.left.equalTo(self.textView.mas_left).offset(-12*KWidthScale);
        make.right.equalTo(self.textView.mas_right).offset(-12);
//        make.width.height.mas_equalTo(20.0f);
    }];

    self.countLab.text=[NSString stringWithFormat:@"%lu/50",self.textView.text.length];
    
    UIButton *huoquBtn = [[UIButton alloc]init];
    huoquBtn.clipsToBounds = YES;
    huoquBtn.layer.cornerRadius =48*KWidthScale/2;
    [huoquBtn addTarget:self action:@selector(huoquBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [huoquBtn setTitle:@"提交" forState:UIControlStateNormal];
    huoquBtn.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    [huoquBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [huoquBtn setBackgroundColor:[UIColor colorWithHex:0xFF729A]];
    [huoquBtn setBackgroundColor: [UIColor colorWithRed:255/255.0 green:114/255.0 blue:154/255.0 alpha:0.5000]];
    [self.view addSubview:huoquBtn];
    
    [huoquBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom).offset(-46*KWidthScale);
        make.left.equalTo(self.view.mas_left).offset(16*KWidthScale);
        make.right.equalTo(self.view.mas_right).offset(-16*KWidthScale);
        make.height.equalTo(@(48*KWidthScale));
    }];
    huoquBtn.userInteractionEnabled=NO;
    self.tjBtn=huoquBtn;
  
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(CPJYXYVCTextDidChange) name:UITextViewTextDidChangeNotification object:nil];
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)CPJYXYVCTextDidChange{
//    self.JYXYStr=self.textView.text;
    if (self.textView.text.length<=50) {
//         self.countLab.text=[NSString stringWithFormat:@"%lu/40",40-self.textView.text.length];
        self.countLab.text=[NSString stringWithFormat:@"%lu/50",self.textView.text.length];
     }else{

         self.textView.text=[self.textView.text substringWithRange:NSMakeRange(0, 50)];
         self.countLab.text=@"50/50";
         [self showSVProgressHUDStr:@"自定义打招呼长度不能超过50位！"];
     }
    
    if (self.isSelect==YES&&[[self.textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]length] >= 2) {
        self.tjBtn.userInteractionEnabled=YES;
        [self.tjBtn setBackgroundColor:[UIColor colorWithHex:0xFF729A]];
    }else{
        self.tjBtn.userInteractionEnabled=NO;
        [self.tjBtn setBackgroundColor: [UIColor colorWithRed:255/255.0 green:114/255.0 blue:154/255.0 alpha:0.5000]];
    }
}
-(void)showSVProgressHUDStr:(NSString *)str{
    if ([str isKindOfClass:[NSNull class]]||str==nil||[str isEqualToString:@""]) {
        return;
    }
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD showImage:[UIImage imageNamed:@""] status:str];
}
-(void)huoquBtnClick{
    [self loadInfoData];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    IQKeyboardManager *keyboardManager =  [IQKeyboardManager sharedManager];
    keyboardManager.enable = YES;
    keyboardManager.enableAutoToolbar = YES;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//    FLBannerModel *model=self.rechargeData[indexPath.row];
    NSString *str=self.reportData[indexPath.row];
    static NSString *identifier = @"FLReportCell";
    PUReportCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    cell.countLab.text=str;
    if (self.isSelect==YES) {
        if (indexPath.item==self.selectItem) {
            cell.bgView.backgroundColor=[UIColor colorWithRed:255/255.0 green:55/255.0 blue:115/255.0 alpha:0.1000];
            cell.countLab.textColor=[UIColor colorWithHex:0xFF3773];
        }else{
            cell.bgView.backgroundColor=[UIColor colorWithRed:136/255.0 green:136/255.0 blue:136/255.0 alpha:0.1000];
            cell.countLab.textColor=[UIColor colorWithHex:0x888888];
        }
    }else{
       
        cell.bgView.backgroundColor=[UIColor colorWithRed:136/255.0 green:136/255.0 blue:136/255.0 alpha:0.1000];
        cell.countLab.textColor=[UIColor colorWithHex:0x888888];
        
    }
  
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.reportData.count;
}

- (CGFloat)minimumInteritemSpacing {
    return 0;
}

//宽136
//高157
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(collectionView.frame.size.width/2,collectionView.frame.size.width*0.106);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0.0f;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0.0f;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
//    return UIEdgeInsetsMake(8, 16, 0, 16);//分别为上、左、下、右
    
    return UIEdgeInsetsMake(0, 0, 0, 0);//分别为上、左、下、右
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld", (long)indexPath.item);
//    BOOL isContain = [self.selectArray containsObject:[NSString stringWithFormat:@"%ld",indexPath.row]];
//    if (isContain) {
//        [self.selectArray removeObject:[NSString stringWithFormat:@"%ld",indexPath.row]];
//    }else{
//        [self.selectArray addObject:[NSString stringWithFormat:@"%ld",indexPath.row]];
//    }
    self.isSelect=YES;
    self.selectItem=indexPath.item;
    [self.collectView reloadData];
    [self CPJYXYVCTextDidChange];
}
-(void)loadInfoData{
   
//    | content | string | 是 | 举报内容 |
//    | pic | array | 是 | 举报截图url集合 |
//    | to_user_id | int | 是| 被举报用户id |
//    | contact | int | 是 | 联系方式 |
//    | contact_type | int | 是 | 联系方式类型（1:手机号；2:号；3:QQ号） |
//    | type | int | 是 | 举报类型(1:涉及政治';2:色情低俗内容';3:未成年';4:传播危害未成年身心健康内容';5:广告营销信息';6:不文明语言';7:诈骗恶意诱导';8:其他') |
//    | scene | int | 是 | 举报场景(1:用户;2:动态)|
    
    NSDictionary *param =@{
                            @"content":self.textView.text,
                            @"to_user_id":@(self.user_id),
                            @"type":@(self.selectItem+1),
                            @"scene":@(1),
                           };
    if (self.scene==2) {
                        param =@{
                                @"content":self.textView.text,
                                @"to_user_id":@(self.user_id),
                                @"type":@(self.selectItem+1),
                                @"scene":@(2),
                               };
    }
    NSString *path=@"/system/report";
    
    NSString *url1 = [NSString stringWithFormat:@"%@%@",baseUrl,path];
//    [[AFNetManager shareManager] postDataWithURLString:url1 path:path parameters:param success:^(NSURLResponse *operation, id successObject) {
//        
//        NSString *result=successObject[@"result"];
//        NSDictionary *dic=@{};
//        if ([successObject[@"result"] isKindOfClass:[NSString class]]) {
//            //解密Base64编码的字符串
//            NSString *decryptedString = [AES128 decryptString:result key:dataKey iv:dataIv];
//            dic=[AES128 dictionaryWithJsonString:decryptedString];
//            NSLog(@"json解析===%@",dic);
//        }
//        
//        if ([successObject[@"code"] integerValue] == 200) {
//            [self showSVProgressHUDStr:@"举报成功"];
//            [self.navigationController popViewControllerAnimated:YES];
//        } else {
//            [self showSVProgressHUDStr:successObject[@"message"]];
//        }
//       
//    } failure:^(NSError *error) {
//      
//        NSLog(@"错误：%@",error);
//        
//    }];
}
@end
